<template>
  <el-card style="height: 80px">
    <el-form :inline="true" class="form">
      <el-form-item label="职位搜索:">
        <el-input placeholder="请输入职位关键字" v-model.trim="keyword"></el-input>
      </el-form-item>
      <el-form-item label="职位搜索:">
        <dict-select dictCode="abc" placeholder="请选择" v-model="selVal"></dict-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="default" :disabled="!keyword" @click="search">搜索</el-button>
        <el-button type="primary" size="default" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin: 10px 0">
    <el-button type="primary" size="default" icon="Plus" @click="addRole">添加职位</el-button>
    <el-table border style="margin: 10px 0" :data="tableData">
      <el-table-column type="index" align="center" label="#"></el-table-column>
      <el-table-column label="ID" align="center" prop="id"></el-table-column>
      <el-table-column label="职位名称" align="center" prop="roleName" show-overflow-tooltip></el-table-column>
      <el-table-column label="创建世间" align="center" show-overflow-tooltip prop="createTime"></el-table-column>
      <el-table-column label="更新时间" align="center" show-overflow-tooltip prop="updateTime"></el-table-column>
      <el-table-column label="操作" width="280px" align="center">
        <!-- row:已有的职位对象 -->
        <template #default="{ row }">
          <el-button type="primary" size="small" icon="User" @click="setPermisstion(row)">分配权限</el-button>
          <el-button type="primary" size="small" icon="Edit" @click="updateRole(row)">编辑</el-button>
          <el-popconfirm :title="`你确定要删除${row.roleName}?`" width="260px" @confirm="removeRole(row.id)">
            <template #reference>
              <el-button type="primary" size="small" icon="Delete">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5, 7, 9, 11]"
      :background="true"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="changeCurrentPage"
      @size-change="changePageSize"
    />
  </el-card>
  <postDialog
    ref="userActionsLogRef"
    :modalData="modalData"
    :modalType="modalType"
    @undated="handlerUndated"
  ></postDialog>
  <AssignPermissionsDialog
    ref="assignPermisstionRef"
    :modalData="modalData"
    @undated="handlerUndated"
  ></AssignPermissionsDialog>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import usePagination from '@/hooks/pagination'
import useSearch from '@/hooks/search'
import { reqAllRoleList } from '@/api/acl/role'
import DictSelect from '@/components/DictSelect/index.vue'
import postDialog from './postDialog/index.vue'
import AssignPermissionsDialog from './AssignPermissionsDialog/index.vue'
let loading = ref<boolean>(false)
let modalType = ref<string>('')
let userActionsLogRef = ref<any>()
let assignPermisstionRef = ref<any>()
//存储全部用户的数组
let tableData = ref<any[]>([])
const modalData = ref<any>({})
const selVal = ref<string>('')
onMounted(() => {
  getTableData()
})
const { keyword, search, reset } = useSearch(() => getTableData())
const { currentPage, pageSize, total, changePageSize, changeCurrentPage } = usePagination(() => getTableData(), {
  defaultPageSize: 5
})
//获取全部已有的用户信息
const getTableData = async () => {
  loading.value = true
  let result: any = await reqAllRoleList(currentPage.value, pageSize.value, keyword.value)
  loading.value = false
  if (result.code === 200) {
    total.value = result.data.total
    tableData.value = result.data.records
  }
}
const handlerUndated = () => {
  getTableData()
}
const addRole = () => {
  modalType.value = 'add'
  modalData.value = null
  userActionsLogRef.value.open()
}
const updateRole = (val: any) => {
  modalType.value = 'edit'
  modalData.value = val
  userActionsLogRef.value.open()
}
const setPermisstion = (val: any) => {
  console.log(val)
  modalData.value = val
  assignPermisstionRef.value.open()
}
const removeRole = (val: any) => {
  console.log(val)
}
</script>

<style scoped>
.form {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
